package com.abc.robot01.vivemodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.abc.robot01.activity.toJSONString
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Data
import com.abc.robot01.retrofit.bean.DataItem
import com.abc.robot01.retrofit.bean.FetchDataRequest
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class HomePageProductViewmodel : ViewModel() {

    val TAG="HomePageProductViewmodel"
    val myLiveData: MutableLiveData<Data?> = MutableLiveData(null)
    val configs: LiveData<Data?> = myLiveData

    fun fetchData(robotId:String , homePageConfigId:String) {
        Log.e(TAG,"fetchData:"+robotId+ "homePageConfigId:"+homePageConfigId)
        viewModelScope.launch {
            try {
                myLiveData.value  = RetrofitService.instance.getHomePageCommodity(FetchDataRequest(0,1000,robotId,homePageConfigId)).data
               Log.e(TAG,"getHomePageCommodity:" + myLiveData.value!!.toJSONString())
            } catch (e: Exception) {
                Log.e(TAG,""+e.message)
                // 处理异常
                e.printStackTrace()
            }
        }
    }
}

